﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDMUI.MaterialUI
{
    public partial class UpMaterial : Form
    {
        public UpMaterial()
        {
            InitializeComponent();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            // 关闭窗体
            this.Close();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            // 获取物料 ID
            int materialId = (int)this.Tag;

            // 创建一个 Material 对象，用于存储修改后的物料信息
            Model.Material updatedMaterial = new Model.Material
            {
                MaterialId = materialId,
                MaterialType = txtMaterialType.Text,
                DetailedMaterialType = txtDetailedMaterialType.Text,
                Quantity = string.IsNullOrEmpty(txtQuantity.Text) ? null : (int?)int.Parse(txtQuantity.Text),
                MaterialUnit = txtMaterialUnit.Text,
                MaterialDescription = txtMaterialDescription.Text,
                ConsumptionDeduction = txtConsumptionDeduction.Text,
                MaterialGroupNumber = txtMaterialGroupNumber.Text
            };

            // 调用 BLL 更新物料信息
            BLL.MaterialBLL.MaterialBLL materialBLL = new BLL.MaterialBLL.MaterialBLL();
            int rowsAffected = materialBLL.UpdateMaterial(updatedMaterial);

            if (rowsAffected > 0)
            {
                MessageBox.Show("物料信息修改成功！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.DialogResult = DialogResult.OK; // 设置对话框结果为 OK，表示修改成功
            }
            else
            {
                MessageBox.Show("物料信息修改失败，请重试！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            this.Close();
        }
    }
}
